package com.yuanfud.blog.service.impl;

import com.yuanfud.blog.VO.NewsVO;
import com.yuanfud.blog.mapper.ArticleMapper;
import com.yuanfud.blog.service.NewsService;
import com.yuanfud.common.exception.RestException;
import com.yuanfud.common.util.ErrorCode;
import com.yuanfud.entity.blog.ArticleList;
import com.yuanfud.entity.common.MasterPage;
import com.yuanfud.entity.common.PageInfoModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("NewsService")
public class NewsServiceImpl implements NewsService {
    @Autowired
    ArticleMapper mapper;

    @Override
    public PageInfoModel<ArticleList> getNewsInfo(MasterPage page) throws RestException {
        if(page.getP()==null)
            page.setP(1);
        if(page.getPnum()==null)
            page.setPnum(10);
        List<NewsVO> list=mapper.getAllNews(page);
        List<ArticleList> result = new ArrayList<>();
        if(list.size()>0) {
            for (NewsVO news : list) {
                ArticleList articleList = new ArticleList();
                articleList.setTitle(news.getTitle());
                articleList.setAuthor(news.getAuthor());
                articleList.setContent(news.getContent());
                articleList.setId(news.getId());
                articleList.setInputDate(news.getInputDate());
                articleList.setInputUser(String.valueOf(news.getInputUser()));
                articleList.setReadNum(news.getReadNum());
                articleList.setNewsImgAddr(news.getNewsImgAddr());
                articleList.setTypeName(getNewsType(news.getType()));
                result.add(articleList);
            }
        }
        com.github.pagehelper.PageInfo pageInfo = new com.github.pagehelper.PageInfo(result);
        PageInfoModel pagination = new PageInfoModel();
        pagination.setTotalPages(pageInfo.getPages());
        pagination.setTotalRows(pageInfo.getTotal());
        pagination.setCurrentPage(pageInfo.getPageNum());

        pagination.setResultList(result);

        return pagination;
    }

    @Override
    public ArticleList getNewsById(int id) throws RestException {
        ArticleList article=mapper.getNewsInfoById(id);
        return article;
    }

    @Override
    public String getNewsType(Integer id) throws RestException {
        String typeName=mapper.getNewsTypeById(id);
        if(typeName==null)
            throw new RestException(ErrorCode.BLOG_ARTICLE_TYPE_ERROR,ErrorCode.BLOG_ARTICLE_TYPE_ERROR_MSG);
        return typeName;
    }

    @Override
    public List<Map<Integer, String>> getNewsTypeList(Integer fatherId) throws RestException {
        List<Map<Integer,String>> list=mapper.getNewsTypeListByFatherId(fatherId);
        if(list.size()==0)
            throw new RestException(ErrorCode.BLOG_ARTICLE_TYPE_ERROR,ErrorCode.BLOG_ARTICLE_TYPE_ERROR_MSG);
        return list;
    }
    @Override
    public boolean addNewsInfo(NewsVO news) throws RestException{
        Integer result=mapper.addNewsInfo(news);
        return result==null?false:true;

    }

    @Override
    public boolean delArticle(int id) throws RestException {
        NewsVO news=new NewsVO();
        news.setId(id);
        news.setDel(true);
        Integer result=mapper.updateNewsInfo(news);
        return result==null?false:true;
    }

    @Override
    public boolean ArticleEdit(NewsVO news) throws RestException {
        Integer result=mapper.updateNewsInfo(news);
        return result==null?false:true;
    }


}
